package com.ciwong.mobilelib.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;

/**
 *
 * @author res-qqlin
 * @version ciwong v.2.0 2015/8/6
 * @since ciwong v.2.0
 */
public class ProgessBeakPoint extends View
{
    protected Paint mPaint = null;// 画笔

    private int point;

    private int currentPoint = 0;

    private Context mContext;

    private final int NORMAL_COLOR = Color.parseColor("#66ececec");

    private final int READ_COLOR = Color.parseColor("#19B200");

    private final int BLANK_COLOR = Color.parseColor("#00000000");

    private int w;

    private Breakpoint[] bps;

    private Handler mHandler = new Handler();

    private boolean init = false;

    public ProgessBeakPoint(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        mContext = context;
        mPaint = new Paint();// 初始化画笔工具
        mPaint.setAntiAlias(true);// 抗锯齿
        mPaint.setColor(Color.BLACK);// 画笔颜色
        mPaint.setStrokeWidth(4);
        // int screenWidth =
        // mContext.getResources().getDisplayMetrics().widthPixels;
        // w = screenWidth;
        // // w = getWidth();
    }

    private void initbreakPoint()
    {
        float an = w * 1.0F / point;
        bps = new Breakpoint[point];
        for (int i = 0; i < point; i++)
        {
            float y = an * i;
            float yto = an * (i + 1);
            bps[i] = new Breakpoint();
            bps[i].setPy(y);
            bps[i].setPyTo(yto);
            bps[i].setIsread(false);
        }
    }

    @Override
    public void draw(Canvas canvas)
    {
        w = getWidth();
        if (init)
        {
            initbreakPoint();
            init = false;
        }
        super.draw(canvas);
        // w = getWidth();
        int y = getHeight() / 2;

        if (bps != null && bps.length > 0)
        {
            for (int i = 0; i < bps.length; i++)
            {
                if (i <= currentPoint)
                {
                    mPaint.setColor(READ_COLOR);
                }
                else
                {
                    mPaint.setColor(NORMAL_COLOR);
                }
                canvas.drawLine(bps[i].getPy(), y, bps[i].getPyTo(), y, mPaint);
                if (i != bps.length - 1)
                {
                    if (i <= currentPoint)
                    {
                        mPaint.setColor(NORMAL_COLOR);
                    }
                    else
                    {
                        mPaint.setColor(BLANK_COLOR);
                    }
                    canvas.drawLine(bps[i].getPyTo() - 2, y, bps[i].getPyTo(),
                            y, mPaint);
                }
            }
        }
        // currentPoint++;
        // mHandler.postDelayed(new Runnable()
        // {
        // @Override
        // public void run()
        // {
        // invalidate();
        // }
        // }, 1000);

    }

    public void reDraw(int p)
    {
        invalidate();
        currentPoint = p;
    }

    public void setPoint(int p)
    {
        point = p;
    }

    public int getPoint()
    {
        return point;
    }

    public int getCurrentPoint()
    {
        return currentPoint;
    }

    public void setInit()
    {
        init = true;
    }

    private class Breakpoint
    {
        float py;

        float pyTo;

        boolean isread;

        public float getPy()
        {
            return py;
        }

        public void setPy(float py)
        {
            this.py = py;
        }

        public float getPyTo()
        {
            return pyTo;
        }

        public void setPyTo(float pyTo)
        {
            this.pyTo = pyTo;
        }

        public boolean isIsread()
        {
            return isread;
        }

        public void setIsread(boolean isread)
        {
            this.isread = isread;
        }

    }
}
